package com.htjc.uep.center.commons.utils;

import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.htjc.uep.center.commons.mapper.SaExtendedMapper;
import com.htjc.uep.center.commons.utils.RSAUtil;
import com.htjc.uep.center.model.entity.SaExtendedConfig;
import com.htjc.uep.center.model.enums.ExtendedConstant;

@Component
public class KeyUtil {

	private static HashMap<String, String> keyMap;
	
	private static ExtendedConstant publickeyConstant = ExtendedConstant.EXTENDED_PUBLIC_KEY;
	
	private static ExtendedConstant privatekeyConstant = ExtendedConstant.EXTENDED_PRIVATE_KEY;
	
	@Autowired
	private SaExtendedMapper mapper;
	
	private void init() {
		keyMap = new HashMap<String, String>();
		
		SaExtendedConfig publicKey = mapper.queryConfigByTypeId(publickeyConstant.getCode(), publickeyConstant.getType(), publickeyConstant.getTypeId());
		SaExtendedConfig privateKey = mapper.queryConfigByTypeId(privatekeyConstant.getCode(), privatekeyConstant.getType(), privatekeyConstant.getTypeId());
		
		if ((publicKey == null || privateKey == null) || (publicKey.getEXT_TYPE_VALUE() == null || privateKey.getEXT_TYPE_VALUE() == null)) {
			throw new RuntimeException("密钥不存在");
		}
		
		keyMap.put(publickeyConstant.getTypeId(), publicKey.getEXT_TYPE_VALUE());
		keyMap.put(privatekeyConstant.getTypeId(), privateKey.getEXT_TYPE_VALUE());
	}
	
	/**
	 * 公钥加密
	 * @param data
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws InvalidKeySpecException
	 */
	public String encryptByPublic(String data) throws NoSuchAlgorithmException, InvalidKeySpecException {
		if (keyMap == null) {
			init();
		}
		RSAPublicKey publicKey = RSAUtil.getPublicKey(keyMap.get(publickeyConstant.getTypeId()));
		return RSAUtil.publicEncrypt(data, publicKey);
	}
	
	/**
	 * 私钥加密
	 * @param data
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws InvalidKeySpecException
	 */
	public String encryptByPrivate(String data) throws NoSuchAlgorithmException, InvalidKeySpecException {
		if (keyMap == null) {
			init();
		}
		RSAPrivateKey privateKey = RSAUtil.getPrivateKey(keyMap.get(privatekeyConstant.getTypeId()));
		return RSAUtil.privateEncrypt(data, privateKey);
	}
	/**
	 * 私钥解密
	 * @param data
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws InvalidKeySpecException
	 */
	public String decryptByPrivate(String data) throws NoSuchAlgorithmException, InvalidKeySpecException {
		if (keyMap == null) {
			init();
		}
		RSAPrivateKey privateKey = RSAUtil.getPrivateKey(keyMap.get(privatekeyConstant.getTypeId()));
		return RSAUtil.privateDecrypt(data, privateKey);
	}
	/**
	 * 公钥解密
	 * @param data
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws InvalidKeySpecException
	 */
	public String decryptByPublic(String data) throws NoSuchAlgorithmException, InvalidKeySpecException {
		if (keyMap == null) {
			init();
		}
		RSAPublicKey publicKey = RSAUtil.getPublicKey(keyMap.get(publickeyConstant.getTypeId()));
		return RSAUtil.publicDecrypt(data, publicKey);
	}
}



































